//
//  HJShareView.m
//  project
//
//  Created by jc on 2019/5/24.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJShareView.h"
#import "YFMobManager.h"

@interface HJShareView ()

/**
 分享背景视图
 */
@property (nonatomic,strong,nonnull) UIView * contentView;

/**
 取消按钮
 */
@property (nonatomic,strong,nonnull) UIButton * cancleBtn;

/**
 分享到
 */
@property (nonatomic,strong,nonnull) UILabel * titleLabel;

/**
 微信好友
 */
@property (nonatomic,strong,nonnull) UIButton * wechatFriend;

/**
 微信朋友圈
 */
@property (nonatomic,strong,nonnull) UIButton * wechatCircle;



@end
@implementation HJShareView


/**
 初始化视图
 @return shareView
 */
+(instancetype)shareView {
    HJShareView * initShareView = [[HJShareView alloc]init];
    initShareView.frame = [[UIScreen mainScreen] bounds];
    return initShareView;
}

/**
 启动弹框
 */
-(void)show {
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformMakeScale(0, 0);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformMakeScale(2.0, 2.0);
        self.contentView.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}
-(void)showWithView:(UIView *)view aboveSubview:(UIView *)aboveSubview {
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformMakeScale(0, 0);
    [view insertSubview:self aboveSubview:aboveSubview];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformMakeScale(2.0, 2.0);
        self.contentView.transform = CGAffineTransformMakeScale(1, 1);
    } completion:^(BOOL finished) {
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}
/**
 隐藏弹框
 */
-(void)dimiss {
   
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.alpha = 0;
        self.contentView.transform = CGAffineTransformMakeScale(0, 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
}

-(instancetype)init {
    if (self = [super init]) {
        [self initView];
    }
    return self;
}

-(void)setIsweb:(BOOL)isweb {
    _contentView.frame = CGRectMake(0, (isweb == NO ? SCREEN_HEIGTH : SCREEN_HEIGTH-Height_NavBar) - 160, SCREEN_WIDTH, 170);
}

-(void)initView {
    // 蒙版
    UIVisualEffectView *ruVisualEffectView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    ruVisualEffectView.frame = [[UIScreen mainScreen] bounds];
    ruVisualEffectView.alpha = 0.3;
    [self addSubview:ruVisualEffectView];
    
    @weakify(self)
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
    [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        @strongify(self)
        [self dimiss];
    }];
    [self addGestureRecognizer:tap];
    
    _contentView = [UIView new];
    _contentView.backgroundColor = [UIColor colorWithHexString:@"1a1a1a"];
    [_contentView rounded:(UIRectCornerTopLeft | UIRectCornerTopRight) radius:10];
    _contentView.frame = CGRectMake(0, (self.isweb == NO ? SCREEN_HEIGTH : SCREEN_HEIGTH-Height_NavBar) - 160, SCREEN_WIDTH, 170);
    [self addSubview:_contentView];
    
    _cancleBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _cancleBtn.frame = CGRectMake(SCREEN_WIDTH - 30, 20, 20, 20);
    [_cancleBtn setImage:[UIImage imageNamed:@"guanbi"] forState:0];
    _cancleBtn.adjustsImageWhenHighlighted = YES;
    [[_cancleBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self dimiss];
    }];
    [_contentView addSubview:_cancleBtn];
    
    _titleLabel = [UILabel new];
    _titleLabel.text = @"分享到";
    _titleLabel.frame = CGRectMake(80, 20, SCREEN_WIDTH - 160, 20);
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font =[UIFont fontWithName:PingFangSC_Medium size:17];
    [_contentView addSubview:_titleLabel];
    
    _wechatFriend = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_wechatFriend setTitle:@"微信好友" forState:0];
    [_wechatFriend setImage:[UIImage imageNamed:@"haoyou"] forState:0];
    _wechatFriend.frame = CGRectMake(SCREEN_WIDTH / 2 - 40 - 48, 40+20, 90, _wechatFriend.currentImage.size.height+30);
    [_wechatFriend setTitleColor:[UIColor whiteColor] forState:0];
    _wechatFriend.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:14];
    
    [self initHorizonAlignmentWithBtn:_wechatFriend];
    [[_wechatFriend rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [[YFMobManager shared] shareWithPlatformType:SSDKPlatformSubTypeWechatSession url:self.shareUrl title:self.title descripte:self.descripte];
        [self dimiss];
    }];
    [_contentView addSubview:_wechatFriend];
    
    _wechatCircle = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_wechatCircle setTitle:@"朋友圈" forState:0];
    [_wechatCircle setImage:[UIImage imageNamed:@"quanzi"] forState:0];
    _wechatCircle.frame = CGRectMake(CGRectGetMaxX(_wechatFriend.frame)+20, 40+20, 90, _wechatCircle.currentImage.size.height+30);
    [_wechatCircle setTitleColor:[UIColor whiteColor] forState:0];
    _wechatCircle.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:14];
    
    [self initHorizonAlignmentWithBtn:_wechatCircle];
    [[_wechatCircle rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [[YFMobManager shared] shareWithPlatformType:SSDKPlatformSubTypeWechatTimeline url:self.shareUrl title:self.title descripte:self.descripte];
        [self dimiss];
    }];
    [_contentView addSubview:_wechatCircle];
    
}

-(void)initHorizonAlignmentWithBtn:(UIButton *)btn {
    CGFloat imageH = btn.imageView.frame.size.height;
    CGFloat imageW = btn.imageView.frame.size.width;
    
    CGFloat titleW = btn.titleLabel.bounds.size.width;
    CGFloat titleH = btn.titleLabel.bounds.size.height;
    
    //使图片和文字水平居中显示
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(imageH+10, -imageW, 0.0f, 0.0f)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0.0f, titleW/2-10, titleH, 0.0f)];
}



@end
